<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Book extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();
        //Load Helper
        $this->load->helper("url");
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->data['is_mobile'] = $this->agent->is_mobile();
        $this->load->library('pagination'); 
        $this->config->set_item('compress_output', TRUE);

        //Load Libary
        $this->load->library("ci_smarty");

        //$this->output->enable_profiler(TRUE);
        //$this->output->cache(30);

        //Load Model
        $this->load->Model("Minfo");
        $this->load->Model("Mcategory");
        $this->load->Model("Mbook");
        $this->load->Model("MPromotion");
        $this->load->Model("MSeo");

        //Load Data
        $this->data['dataAll'] = $this->Mcategory->getCategoriesByType(WEB_TYPE);
        $this->data['infoAll'] = $this->Minfo->getInfo();
        $this->data['bookBestList'] = $this->Mbook->getBookPopularBySumViewChapter();
        $this->data['promotion1'] = $this->MPromotion->getPromotionByPos(1);
        $this->data['promotion2'] = $this->MPromotion->getPromotionByPos(2);
        $this->data['seoData'] = $this->MSeo->getCommonSEO(); //Luôn có ở tất cả controller
    }

    public function listBookAll()
    {
        //Load Category
        $category = $this->Mcategory->getCategoryByUname($cate_uname);
        if (count($category) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['category'] = $category;

        $book = $this->Mbook->getBook();
        if (count($book) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['listBookAll'] = $book;
        $this->ci_smarty->view('frontend/list-view.tpl',$this->data); 
    }

    public function listBook($cate_uname, $curPage = 1)
    {
        $this->data['main_nav_selected'] = $cate_uname;

        //Load Category
        $category = $this->Mcategory->getCategoryByUname($cate_uname);
        if (count($category) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['category'] = $category;

        // config page
        $bookPageConfig['base_url']      = base_url("truyen-$cate_uname/");
        $bookPageConfig['total_rows']    = $this->Mbook->countBookByCategoryID($category['id']);
        $bookPageConfig['per_page']      = 20;
        $bookPageConfig['uri_segment']   = 2;
        $bookPageConfig['cur_page']      = $curPage;
        $bookPageConfig['first_url']     = $this->data['base_url'] . 'truyen-' . $category['uname'] . '.html';

        $curOffset = $bookPageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($bookPageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        //Load Book List
        $book = $this->Mbook->getBooksByCategoryUname($cate_uname, $bookPageConfig['per_page'],$curOffset);
        $this->data['listBook'] = $book;

        // seo book template
        if($curPage > 1){
            $categoryLink = $this->data['base_url'] . 'truyen-' . $category['uname'] . '/trang-'.$curPage.'.html';
        }else{
            $categoryLink = $this->data['base_url'] . 'truyen-' . $category['uname'] . '.html';
        }
        $title = $category['page_title'];
        $keyword= $category['page_keyword'];
        $description = $category['page_description'];
        
        $this->data['seoData']['title'] = $title;
        $this->data['seoData']['description'] = $description;
        $this->data['seoData']['news_keywords'] = $keyword;
        $this->data['seoData']['canonical'] = $categoryLink;
 
        $this->data['seoData']['og:url'] = $categoryLink;
        $this->data['seoData']['og:title'] = $title;
        $this->data['seoData']['description'] = $description;  
        
        $this->data['seoTemplate'] = $this->MSeo->getTemplateSEO($this->data['seoData']);
        $this->ci_smarty->view('frontend/list-view.tpl',$this->data); 
    }

    public function gridBook($cate_uname)
    {
        $this->data['main_nav_selected'] = $cate_uname;

        //Load Category
        $category = $this->Mcategory->getCategoryByUname($cate_uname);
        if (count($category) == 0) {
            $this->ci_smarty->view404();
            return false;
        }
        $this->data['category'] = $category;

        //Load Book List
        $book = $this->Mbook->getBooksByCategoryUname($cate_uname);
        $this->data['listBook'] = $book;
        $this->ci_smarty->view('frontend/grid-view.tpl',$this->data); 
    }
    
    public function searchBook()
    {

        $book_name = $this->input->post('book_name');

        //Load Book List
        $book = $this->Mbook->getBooksByLikeName($book_name);
        $this->data['listBook'] = $book;
	
        $this->data['seoTemplate'] = $this->MSeo->getTemplateSEO($this->data['seoData']);
        $this->ci_smarty->view('frontend/list-view.tpl',$this->data); 
    }
}